home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / f_rotate.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  5.7 KB  |  234 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <stdio.h>
  19. #include <crtdbg.h>
  20.  
  21. #include <windows.h>
  22. #include <commctrl.h>
  23.  
  24. #include "ScriptInterpreter.h"
  25. #include "ScriptValue.h"
  26. #include "ScriptError.h"
  27.  
  28. #include "resource.h"
  29. #include "filter.h"
  30.  
  31. extern HINSTANCE g_hInst;
  32.  
  33. enum {
  34.     MODE_LEFT90 = 0,
  35.     MODE_RIGHT90 = 1,
  36.     MODE_180 = 2
  37. };
  38.  
  39. static const char *const g_szMode[]={
  40.     "left 90\xb0",
  41.     "right 90\xb0",
  42.     "180\xb0",
  43. };
  44.  
  45. typedef struct MyFilterData {
  46.     int mode;
  47. } MyFilterData;
  48.  
  49. ///////////////////////////////////////////////////////////////////////////
  50.  
  51. static int rotate_run(const FilterActivation *fa, const FilterFunctions *ff) {
  52.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  53.     Pixel32 *src, *dst, *dst0;
  54.     PixDim w0 = fa->src.w, w;
  55.     PixDim h = fa->src.h;
  56.     PixOffset dpitch = fa->dst.pitch;
  57.     PixOffset dmodulo = fa->dst.modulo;
  58.     PixOffset smodulo = fa->src.modulo;
  59.  
  60.     switch(mfd->mode) {
  61.     case MODE_LEFT90:
  62.         src = fa->src.data;
  63.         dst0 = fa->dst.data + fa->dst.w;
  64.  
  65.         do {
  66.             dst = --dst0;
  67.             w = w0;
  68.             do {
  69.                 *dst = *src++;
  70.                 dst = (Pixel32*)((char *)dst + dpitch);
  71.             } while(--w);
  72.  
  73.             src = (Pixel32*)((char *)src + smodulo);
  74.         } while(--h);
  75.         break;
  76.  
  77.     case MODE_RIGHT90:
  78.         src = fa->src.data;
  79.         dst0 = (Pixel32 *)((char *)fa->dst.data + fa->dst.pitch*(fa->dst.h-1));
  80.  
  81.         do {
  82.             dst = dst0++;
  83.             w = w0;
  84.             do {
  85.                 *dst = *src++;
  86.                 dst = (Pixel32*)((char *)dst - dpitch);
  87.             } while(--w);
  88.  
  89.             src = (Pixel32*)((char *)src + smodulo);
  90.         } while(--h);
  91.         break;
  92.  
  93.     case MODE_180:
  94.         src = fa->src.data;
  95.         dst = (Pixel32 *)((char *)fa->dst.data + fa->dst.pitch*(fa->dst.h-1) + fa->dst.w*4 - 4);
  96.  
  97.         h>>=1;
  98.         if (h) do {
  99.             w = w0;
  100.             do {
  101.                 Pixel32 a, b;
  102.  
  103.                 a = *src;
  104.                 b = *dst;
  105.  
  106.                 *src++ = b;
  107.                 *dst-- = a;
  108.             } while(--w);
  109.  
  110.             src = (Pixel32*)((char *)src + smodulo);
  111.             dst = (Pixel32*)((char *)dst - dmodulo);
  112.         } while(--h);
  113.  
  114.         // if there is an odd line, flip half of it
  115.  
  116.         if (fa->src.h & 1) {
  117.             w = w0>>1;
  118.             if (w) do {
  119.                 Pixel32 a, b;
  120.  
  121.                 a = *src;
  122.                 b = *dst;
  123.  
  124.                 *src++ = b;
  125.                 *dst-- = a;
  126.             } while(--w);
  127.         }
  128.         break;
  129.     }
  130.     return 0;
  131. }
  132.  
  133. static long rotate_param(FilterActivation *fa, const FilterFunctions *ff) {
  134.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  135.  
  136.     if (mfd->mode == MODE_180)
  137.         return 0;
  138.  
  139.     fa->dst.w = fa->src.h;
  140.     fa->dst.h = fa->src.w;
  141.     fa->dst.AlignTo8();
  142.  
  143.     return FILTERPARAM_SWAP_BUFFERS;
  144. }
  145.  
  146. static BOOL APIENTRY rotateDlgProc( HWND hDlg, UINT message, UINT wParam, LONG lParam) {
  147.     switch (message)
  148.     {
  149.         case WM_INITDIALOG:
  150.             {
  151.                 MyFilterData *mfd = (MyFilterData *)lParam;
  152.                 SetWindowLong(hDlg, DWL_USER, (LONG)mfd);
  153.  
  154.                 switch(mfd->mode) {
  155.                 case MODE_LEFT90:    CheckDlgButton(hDlg, IDC_ROTATE_LEFT, BST_CHECKED); break;
  156.                 case MODE_RIGHT90:    CheckDlgButton(hDlg, IDC_ROTATE_RIGHT, BST_CHECKED); break;
  157.                 case MODE_180:        CheckDlgButton(hDlg, IDC_ROTATE_180, BST_CHECKED); break;
  158.                 }
  159.             }
  160.             return (TRUE);
  161.  
  162.         case WM_COMMAND:                      
  163.             if (LOWORD(wParam) == IDOK) {
  164.                 MyFilterData *mfd = (struct MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  165.  
  166.                 if (IsDlgButtonChecked(hDlg, IDC_ROTATE_LEFT)) mfd->mode = MODE_LEFT90;
  167.                 if (IsDlgButtonChecked(hDlg, IDC_ROTATE_RIGHT)) mfd->mode = MODE_RIGHT90;
  168.                 if (IsDlgButtonChecked(hDlg, IDC_ROTATE_180)) mfd->mode = MODE_180;
  169.  
  170.                 EndDialog(hDlg, 0);
  171.                 return TRUE;
  172.             } else if (LOWORD(wParam) == IDCANCEL) {
  173.                 EndDialog(hDlg, 1);  
  174.                 return TRUE;
  175.             }
  176.             break;
  177.     }
  178.     return FALSE;
  179. }
  180.  
  181. static int rotate_config(FilterActivation *fa, const FilterFunctions *ff, HWND hWnd) {
  182.     return DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_FILTER_ROTATE), hWnd, rotateDlgProc, (LONG)fa->filter_data);
  183. }
  184.  
  185. static void rotate_string(const FilterActivation *fa, const FilterFunctions *ff, char *buf) {
  186.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  187.  
  188.     wsprintf(buf, " (%s)", g_szMode[mfd->mode]);
  189. }
  190.  
  191. static void rotate_script_config(IScriptInterpreter *isi, void *lpVoid, CScriptValue *argv, int argc) {
  192.     FilterActivation *fa = (FilterActivation *)lpVoid;
  193.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  194.  
  195.     mfd->mode    = argv[0].asInt();
  196.  
  197.     if (mfd->mode < 0 || mfd->mode > 2)
  198.         mfd->mode = 0;
  199. }
  200.  
  201. static ScriptFunctionDef rotate_func_defs[]={
  202.     { (ScriptFunctionPtr)rotate_script_config, "Config", "0i" },
  203.     { NULL },
  204. };
  205.  
  206. static CScriptObject rotate_obj={
  207.     NULL, rotate_func_defs
  208. };
  209.  
  210. static bool rotate_script_line(FilterActivation *fa, const FilterFunctions *ff, char *buf, int buflen) {
  211.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  212.  
  213.     _snprintf(buf, buflen, "Config(%d)", mfd->mode);
  214.  
  215.     return true;
  216. }
  217.  
  218. FilterDefinition filterDef_rotate={
  219.     0,0,NULL,
  220.     "rotate",
  221.     "Rotates an image by 90, 180, or 270 degrees.",
  222.     NULL,NULL,
  223.     sizeof(MyFilterData),
  224.     NULL,NULL,
  225.     rotate_run,
  226.     rotate_param,
  227.     rotate_config,
  228.     rotate_string,
  229.     NULL,
  230.     NULL,
  231.  
  232.     &rotate_obj,
  233.     rotate_script_line,
  234. };